package demo1.servlet.all;

import com.alibaba.fastjson.JSON;
import demo1.pojo.users;
import demo1.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/login")
public class login extends HttpServlet {//登录
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为user对象
        users users = JSON.parseObject(params,users.class);
        //调用service验证
        userService check = new userService();
        int num = -1;
        try {
            num = check.checkLogin(users);//判断有没有用户，有返回1，没有返回0
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(num == 1){
            //手机号登录
            try {
                Cookie c_username = new Cookie("username",users.getUserName());
                Cookie c_password = new Cookie("password",users.getUserPwd());
                c_username.setMaxAge(60*60*24*7);
                c_password.setMaxAge(60*60*24*7);
                resp.addCookie(c_password);
                resp.addCookie(c_username);
                users = check.loginUserByPhone(users);
                if(users.getPanduan() == 1){//登录商户
                    HttpSession session = req.getSession();
                    session.setAttribute("users", users);
                    resp.getWriter().write("success1");

                }
                if(users.getPanduan() == 0){//登录用户
                    HttpSession session = req.getSession();
                    session.setAttribute("users", users);
                    resp.getWriter().write("success2");
                }
                if(users.getPanduan() == 3){//被封号
                    resp.getWriter().write("error1");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        else if(num == 2){
            //账号登录
            try {

                Cookie c_username = new Cookie("username",users.getUserName());
                Cookie c_password = new Cookie("password",users.getUserPwd());
                c_username.setMaxAge(60*60*24*7);
                c_password.setMaxAge(60*60*24*7);
                resp.addCookie(c_password);
                resp.addCookie(c_username);
                users = check.loginUserBuyAccount(users);
                if(users.getPanduan() == 1){//登录商户
                    HttpSession session = req.getSession();
                    session.setAttribute("users", users);
                    resp.getWriter().write("success1");
                }
                if(users.getPanduan() == 0){//登录用户
                    HttpSession session = req.getSession();
                    session.setAttribute("users", users);
                    resp.getWriter().write("success2");
                }
                if(users.getPanduan() == 2){//登录管理端
                    HttpSession session = req.getSession();
                    session.setAttribute("users", users);
                    resp.getWriter().write("success3");
                }
                if(users.getPanduan() == 3){//被封号
                    resp.getWriter().write("error1");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        else {
            resp.getWriter().write("false");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
